const app = getApp();
var url = app.data.url;
import Dialog from '@vant/weapp/dialog/dialog';
import Toast from '@vant/weapp/toast/toast';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    codeDesc:'',
    time:'',
    deskInfo:'',
    orderNo:'',
    itemInfo:'',
    orderId:'',
    payId: '',
    downOrderTime: '',
    status: '',
    preNum: '',
    itemInfoList:[],
    deskInfoList:[],
    order_mark:"订单备注 (偏好、口味)",
    totalPrice:"",
    queryType: '',
    name:'',
    phone:'',
    totalPrice:'',
    orderType: '',
    orderTypeStr:'',
    mark: '',
    address:'',
    tableFee:'',
    peopleNum: '',
    totalTableFee: '',
    inHallPreMenu:'',
    totalPrice: 0
  },
  onClickLeft() {
    wx.switchTab({
      url: '../../pages/index/index'
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("=================");
    console.log(options);
    console.log("=================");
    wx.setNavigationBarTitle({
      title: "我的订单"
    })
    var orderId = options.orderId;
    var queryType = options.queryType;
    var isShareByJielong = options.isShareByJielong;
    var jielongPayOrderId = options.jielongPayOrderId;
    var that = this;
    that.setData({
      orderId: orderId,
      queryType: queryType,
      codeDesc: isShareByJielong == 'Y' ? '支付码' : '支付码'
    })
    if (queryType == 0){
      wx.request({
        url: url + '/OrderApi/queryOrderInfoByOrderId',
        data: { orderId: orderId, isShareByJielong: isShareByJielong, jielongPayOrderId: jielongPayOrderId},
        method: 'POST',
        header: { 'content-type': 'application/x-www-form-urlencoded' },
        success: function (res) {
          console.log(res);
          if (res.data.code == 200) {
            let item = res.data.data;
            that.setData({
              time: item.timeInfo,
              deskInfo: item.deskInfo,
              orderNo: item.showOrderId,
              itemInfo: item.itemInfo,
              itemInfoList: item.itemInfoList,
              totalPrice: item.price,
              payId: item.payId,
              downOrderTime: item.downOrderTime,
              status: item.status,
              preNum: item.preNum,
              name: item.name,
              phone: item.phone,
              totalPrice: item.price,
              orderType: item.orderType,
              inHallPreMenu: item.hallInType,
              mark: item.mark,
              address: item.address,
              tableFee: item.tableFee,
              totalTableFee: parseFloat(item.totalTableFee),
              rate: item.rate,
              peopleNum: item.peopleNum,
              price: parseFloat(item.totalPrice)
            });
            let orderType = item.orderType;
            let tempTitle = "";
            if (orderType == 'SUBSCRIBE_IN_HALL') {
              tempTitle = "预约餐桌"
            } else if (orderType == 'PICK_UP') {
              tempTitle = "自提"
            } else if (orderType == 'TAKE_OUT') {
              tempTitle = "商家配送"
            } else if (orderType == 'IN_HALL') {
              tempTitle = "堂食"
            } else if (orderType == 'jielong'){
              tempTitle = "接龙点餐"
            }
            that.setData({
              orderTypeStr: tempTitle
            })
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: "none",
              duration: 2000
            })
          }
        },
        fail: function (res) {
          console.info('submit fail');
        },
        complete: function (res) {
          console.info('submit complete');
        }
      }) 
    } else {
      console.log("444");
      wx.request({
        url: url + '/OrderApi/queryOrderInfoByOrderId',
        data: { orderId: orderId },
        method: 'POST',
        header: { 'content-type': 'application/x-www-form-urlencoded' },
        success: function (res) {
          console.log(res);
          if (res.data.code == 200) {
            let item = res.data.data;
            that.setData({
              time: item.timeInfo,
              deskInfo: item.deskInfo,
              orderNo: item.showOrderId,
              itemInfo: item.itemInfo,
              itemInfoList: item.itemInfoList,
              totalPrice: item.price,
              payId: item.payId,
              downOrderTime: item.downOrderTime,
              status: item.status,
              name: item.name,
              phone: item.phone,
              preNum: item.preNum == null?'':item.preNum
            });
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: "none",
              duration: 2000
            })
          }
        },
        fail: function (res) {
          console.info('submit fail');
        },
        complete: function (res) {
          console.info('submit complete');
        }
      }) 
    } 
  },
  /**
 * 退订
 */
  unSubscribeOrder: function (option) {
    Dialog.confirm({
      message: '是否需要退订？'
    }).then(() => {
      // 确定退订
      var that = this;
      wx.request({
        url: url + '/OrderApi/unSubscribeOrderByOrderId',
        data: {
          orderId: that.data.orderId
        },
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          console.log(res);
          if (res.data.code == 200) {
            Toast.success('退订成功!');
            wx.switchTab({
              url: '../../pages/index/index'
            });
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: "none",
              duration: 2000
            })
          }
        },
        fail: function (res) {
          console.info('submit fail');
        },
        complete: function (res) {
          console.info('submit complete');
        }
      })
    }).catch(() => {
      // on cancel
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})